﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using it_security_project.App_Code.BusinessLogicLayer;
using it_security_project.App_Code;

namespace it_security_project
{
    public partial class CreateThread : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                GenerateCaptcha();  
            }
            
        }

        public new Site site { get { return base.Master as Site; } }

        private Captcha c = new Captcha();

        protected override void OnInit(EventArgs e)
        {
            Member member = site.LoginWithSession();
            if (member == null) // User is NOT logged in
            {
                Response.Redirect("~/Forum.aspx");
            }
        }

        private void GenerateCaptcha()
        {          
            c = new CaptchaGenerator().GetCaptcha();
            
            ImageCaptacha.ImageUrl = c.ImageURL;
            FUUUSK.Text = c.Name;
        }

        private string ValidationGroup = "loginvalidation";

        protected void SubmitThreadButton_Click(object sender, EventArgs e)
        {
            try
            {
                if (TextBoxCaptcha.Text.ToLower() == FUUUSK.Text.ToLower())
                {
                    if (TitleTextbox.Text != "" && ContentTextBox.Text != "")
                    {
                        string title = TitleTextbox.Text.Trim();
                        if (title.Length > 5)
                        {
                            if (!string.IsNullOrWhiteSpace(TitleTextbox.Text) || TitleTextbox.Text.Length > 5 && TitleTextbox.Text.Length < 30)
                            {
                                if (!string.IsNullOrWhiteSpace(ContentTextBox.Text) || ContentTextBox.Text.Length < 500 && ContentTextBox.Text.Length > 25)
                                {
                                    Thread thread = new Thread();
                                    thread.Title = TitleTextbox.Text;
                                    thread.Topic = ContentTextBox.Text;
                                    //@TODO make author to logged in user
                                    string username = site.GetMemberName();
                                    if (username != null)
                                    {
                                        thread.Author = username;
                                        Service service = new Service();
                                        service.AddThread(thread);

                                        Response.Redirect("~/Forum.aspx");
                                    }
                                    else
                                    {
                                        PageExtensions.AddErrorMessage(this, "You are not logged in!", ValidationGroup);
                                    }
                                }
                                else
                                {
                                    PageExtensions.AddErrorMessage(this, "Content length needs to be larger then 25 and smaller then 500!", ValidationGroup);
                                }

                            }
                            else
                            {
                                PageExtensions.AddErrorMessage(this, "Title length needs to be larger then 5 and smaller then 30!", ValidationGroup);
                            }
                        }
                        else
                        {
                            PageExtensions.AddErrorMessage(this, "The title must contain atleast 5 characters which is not whitespace.", ValidationGroup);
                        }
                    }
                    else
                    {
                        PageExtensions.AddErrorMessage(this, "Need to provide Title and Content", ValidationGroup);
                    }
                }
                else
                {
                    PageExtensions.AddErrorMessage(this, "Captcha is wrong.", ValidationGroup);
                }
            }
            catch (Exception ex)
            {
                Service service = new Service();
                Member member = site.LoginWithSession();
                if (member != null)
                {
                    service.AddLog(member.MemberID, "Error occured when trying to create post: " + ex);
                }
                else
                {
                    service.AddLog(-1, "Something has gone wrong, error while trying to post a thread and no user logged in.");
                }
            }
            
        }
    }
}